package tool

import (
	"gin/CloudRestaurant/model"
	_ "github.com/go-sql-driver/mysql"
	"github.com/go-xorm/xorm"
)

var DBEngine *Orm
type Orm struct {
	*xorm.Engine
}

func OrmEngine(cfg *Config)(*Orm,error)  {
	// 配置文件读取 数据库操作引擎哦你的启动
	database := cfg.Database
	conn := database.User + ":" + database.Password + "@tcp(" + database.Host + ":" + database.Port + ")/" + database.DbName + "?charset=" + database.Charset
	enging,err := xorm.NewEngine(database.Driver,conn)
	if err != nil{
		return nil, err
	}

	// orm相关设置
	enging.ShowSQL(database.ShowSql)

	// 模型的注册
	err =enging.Sync2(new(model.SmsCode),new(model.Member),new(model.Student),new(model.User))
	if err!=nil {
		return nil,err
	}


	orm := new(Orm)
	orm.Engine = enging

	DBEngine = orm
	return orm,nil
}
